using System.Data;

namespace Caculator
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            btn0.Click += new EventHandler(btnNumberOrOperater_Click);
            btn1.Click += new EventHandler(btnNumberOrOperater_Click);
            btn2.Click += new EventHandler(btnNumberOrOperater_Click);
            btn3.Click += new EventHandler(btnNumberOrOperater_Click);
            btn4.Click += new EventHandler(btnNumberOrOperater_Click);
            btn5.Click += new EventHandler(btnNumberOrOperater_Click);
            btn6.Click += new EventHandler(btnNumberOrOperater_Click);
            btn7.Click += new EventHandler(btnNumberOrOperater_Click);
            btn8.Click += new EventHandler(btnNumberOrOperater_Click);
            btn9.Click += new EventHandler(btnNumberOrOperater_Click);
            btnAdd.Click += new EventHandler(btnNumberOrOperater_Click);
            btnMinus.Click += new EventHandler(btnNumberOrOperater_Click);
            btnMultiply.Click += new EventHandler(btnNumberOrOperater_Click);
            btnDivide.Click += new EventHandler(btnNumberOrOperater_Click);
            btnLeftBracket.Click += new EventHandler(btnNumberOrOperater_Click);
            btnRightBracket.Click += new EventHandler(btnNumberOrOperater_Click);
            btnEquals.Click += new EventHandler(btnEquals_Click);
            btnEmpty.Click += new EventHandler(btnEmpty_Click);
            btnBackspace.Click += new EventHandler(btnBackspace_Click);
        }
        private string expression;
        private void btnNumberOrOperater_Click(object sender, EventArgs e)
        {
            Button btn = sender as Button;
            if (btn != null)
            {
                expression += btn.Text;
                textBox1.Text = expression;
            }
        }
        private void btnEquals_Click(object sender, EventArgs e)
        {
            try
            {
                double result = getval(expression);
                expression = result.ToString();
                textBox1.Text = expression;
            }
            catch (Exception ex)
            {
                textBox1.Text = "Error";
            }
        }
        private void btnBackspace_Click(object sender, EventArgs e)
        {
            try
            {
                if (expression != null)
                {
                    expression = expression.Substring(0, expression.Length - 1);
                    textBox1.Text = expression;
                }
            }
            catch (Exception)
            {

                throw;
            }

        }
        private void btnEmpty_Click(object sender, EventArgs e)
        {
            expression = "";
            textBox1.Text = expression;
        }
        private double getval(string expression)
        {
            DataTable table = new DataTable();
            var result = table.Compute(expression, "");
            return Convert.ToDouble(result);
        }
    }
}